1 using ProceduralToolkit.Examples.UI;
2 using UnityEngine;
3
4 namespace ProceduralToolkit.Examples
5 {
6 public class ChairGeneratorConfigurator : ConfiguratorBase
7 {
8 public MeshFilter chairMeshFilter;
9 public MeshFilter platformMeshFilter;
10 public RectTransform leftPanel;
11 public bool constantSeed = false;
12 public ChairGenerator.Config config = new ChairGenerator.Config();
13
14 private const float minLegWidth = 0.05f;
15 private const float maxLegWidth = 0.12f;
16 private const float minLegHeight = 0.5f;
17 private const float maxLegHeight = 1.2f;
18
19 private const float minSeatWidth = 0.5f;
20 private const float maxSeatWidth = 1.2f;
21 private const float minSeatDepth = 0.3f;
22 private const float maxSeatDepth = 1.2f;
23 private const float minSeatHeight = 0.03f;
24 private const float maxSeatHeight = 0.2f;
25
26 private const float minBackHeight = 0.5f;
27 private const float maxBackHeight = 1.3f;
28
29 private const float platformHeight = 0.05f;
30 private const float platformRadiusOffset = 0.5f;
31
32 private Mesh chairMesh;
33 private Mesh platformMesh;
34
35 private void Awake()
36 {
37 Generate();
38 SetupSkyboxAndPalette();
39
40 InstantiateControl<SliderControl>(leftPanel)
41 .Initialize("Leg width", minLegWidth, maxLegWidth, config.legWidth, value =>
42 {
43 config.legWidth = value;
44 Generate();
45 });
46 InstantiateControl<SliderControl>(leftPanel)
47 .Initialize("Leg height", minLegHeight, maxLegHeight, config.legHeight, value =>
48 {
49 config.legHeight = value;
50 Generate();
51 });
52
53 InstantiateControl<SliderControl>(leftPanel)
54 .Initialize("Seat width", minSeatWidth, maxSeatWidth, config.seatWidth, value =>
55 {
56 config.seatWidth = value;
57 Generate();
58 });
59 InstantiateControl<SliderControl>(leftPanel)
60 .Initialize("Seat depth", minSeatDepth, maxSeatDepth, config.seatDepth, value =>
61 {
62 config.seatDepth = value;
63 Generate();
64 });
65 InstantiateControl<SliderControl>(leftPanel)
66 .Initialize("Seat height", minSeatHeight, maxSeatHeight, config.seatHeight, value =>
67 {
68 config.seatHeight = value;
69 Generate();
70 });
71
72 InstantiateControl<SliderControl>(leftPanel)
73 .Initialize("Back height", minBackHeight, maxBackHeight, config.backHeight, value =>
74 {
75 config.backHeight = value;
76 Generate();
77 });
78
79 InstantiateControl<ToggleControl>(leftPanel).Initialize("Has stretchers", config.hasStretchers, value =>
80 {
81 config.hasStretchers = value;
82 Generate();
83 });
84 InstantiateControl<ToggleControl>(leftPanel).Initialize("Has armrests", config.hasArmrests, value =>
85 {
86 config.hasArmrests = value;
87 Generate();
88 });
89
90 InstantiateControl<ButtonControl>(leftPanel).Initialize("Generate", () => Generate());
91 }
92
93 private void Update()
94 {
95 UpdateSkybox();
96 }
97
98 public void Generate(bool randomizeConfig = true)
99 {
100 if (constantSeed)
101 {
102 Random.InitState(0);
103 }
104
105 if (randomizeConfig)
106 {
107 GeneratePalette();
108
109 config.color = GetMainColorHSV().ToColor();
110 }
111
112 var chairDraft = ChairGenerator.Chair(config);
113 AssignDraftToMeshFilter(chairDraft, chairMeshFilter, ref chairMesh);
114
115 float chairRadius = Mathf.Sqrt(config.seatWidth*config.seatWidth/4 + config.seatDepth*config.seatDepth/4);
116 float platformRadius = chairRadius + platformRadiusOffset;
117
118 var platformDraft = Platform(platformRadius, platformHeight);
119 AssignDraftToMeshFilter(platformDraft, platformMeshFilter, ref platformMesh);
120 }
121 }
122 }